package com.yy.controller.systemmanager;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yy.common.core.AjaxResult;
import com.yy.common.core.HttpStatus;
import com.yy.common.core.model.UserRestaurantVM;
import com.yy.common.core.page.TableDataInfo;
import com.yy.common.utils.PageHelper;
import com.yy.pojo.Restaurant;
import com.yy.pojo.User;
import com.yy.pojo.UserRest;
import com.yy.service.RestaurantService;
import com.yy.service.UserRestService;
import com.yy.service.UserService;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/system-manager/auth-distribute")
public class AuthDistributeController {

    @Autowired
    private RestaurantService restaurantService;
    @Autowired
    private UserRestService userRestService;
    @Autowired
    private UserService userService;

    @GetMapping("/restaurant-query")
    public AjaxResult restaurantQuery() {
        List<Restaurant> restaurantList = restaurantService.list();
        return AjaxResult.success(restaurantList);
    }

    @GetMapping("/restaurant-lead")
    public TableDataInfo restaurantLead(@RequestParam(value = "pageNum", defaultValue = "1") int pageNum,
                                        @RequestParam(value = "pageSize", defaultValue = "10") int pageSize,
                                        User user) {

        IPage<User> page = userService.getUsersList(pageNum, pageSize, user);
        List<UserRest> userRestList = userRestService.list();
        Page<UserRestaurantVM> userRestaurantVMPage = PageHelper.copyMap(page, u -> {
            UserRestaurantVM userRestaurantVM = new UserRestaurantVM();
            userRestaurantVM.setUserId(u.getUserId());
            userRestaurantVM.setUserName(u.getUserName());
            userRestaurantVM.setAvatar(u.getAvatar());
            userRestaurantVM.setPhonenumber(u.getPhonenumber());
            userRestaurantVM.setNickName(u.getNickName());
            userRestaurantVM.setUserType(u.getUserType());
            userRestList.stream().filter(e -> u.getUserId().equals(e.getUserId())).findFirst().ifPresent(
                    e -> {
                        userRestaurantVM.setRestaurant(restaurantService.getById(e.getRestId()));
                    }
            );
            return userRestaurantVM;
        });

        TableDataInfo rspData = new TableDataInfo();
        rspData.setCode(HttpStatus.SUCCESS);
        rspData.setMsg("查询成功");
        rspData.setRows(userRestaurantVMPage.getRecords());
        rspData.setTotal(userRestaurantVMPage.getTotal());
        return rspData;
    }


    @PostMapping("/add")
    public AjaxResult add(@RequestBody UserRest userRest) {
        if (ObjectUtils.isNotEmpty(userRest.getUserId()) || ObjectUtils.isNotEmpty(userRest.getRestId())) {

            boolean save = userRestService.save(userRest);
            if (save) {
                return AjaxResult.success();
            }
        }
        return AjaxResult.error();
    }

    @DeleteMapping("/delete")
    public AjaxResult delete(UserRest userRest) {
        if (ObjectUtils.isNotEmpty(userRest.getUserId()) || ObjectUtils.isNotEmpty(userRest.getRestId())) {

            boolean remove = userRestService.remove(Wrappers.lambdaQuery(UserRest.class)
                    .eq(UserRest::getRestId, userRest.getRestId())
                    .eq(UserRest::getUserId, userRest.getUserId()));
            if (remove) {
                return AjaxResult.success();
            }
        }
        return AjaxResult.error();
    }

    @PutMapping("update")
    public AjaxResult update(@RequestBody UserRest userRest) {
        if (ObjectUtils.isNotEmpty(userRest.getUserId()) || ObjectUtils.isNotEmpty(userRest.getRestId())) {
            boolean update = userRestService.update(Wrappers.lambdaUpdate(UserRest.class)
                    .eq(UserRest::getUserId, userRest.getUserId())
                    .set(UserRest::getRestId, userRest.getRestId()));
            if (update) {
                return AjaxResult.success();
            }
        }
        return AjaxResult.error();
    }


}
